//
//  RTSearchView.m
//  WeiboSina
//
//  Created by Brisinga on 16/3/4.
//  Copyright © 2016年 Root. All rights reserved.
//

#import "RTSearchView.h"

@interface RTSearchView ()

@property (nonatomic, strong) UITextField *searchField;

@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, strong) UIImageView *leftView;

@end

@implementation RTSearchView


- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


#pragma mark - 初始化
- (void)setupUI {
    
    
    self.searchField.leftView = self.leftView;
    
//    [self bringSubviewToFront:self.searchField];
    
    
    // 添加监听事件
    [self.searchField addTarget:self action:@selector(searchFieldDidEdit:) forControlEvents:UIControlEventEditingDidBegin];

    [self.cancelBtn addTarget:self action:@selector(cancelBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark - 搜索框编辑事件
- (void)searchFieldDidEdit:(UITextField *)sender {

    [self.searchField becomeFirstResponder];
    
    
    
//    [self.searchField mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.right.mas_equalTo(self.cancelBtn.width * -1);
//        
//    }];
    
   [UIView animateWithDuration:0.25 animations:^{
       
       self.searchField.width = self.width - self.cancelBtn.width;
       
   } completion:^(BOOL finished) {
       
       self.cancelBtn.hidden = NO;
   }];
}


#pragma  mark - 取消按钮点击事件
- (void)cancelBtnDidClick:(UIButton *)sender {
    
    self.cancelBtn.hidden = YES;
    
    [self.searchField resignFirstResponder];
    
    [self.searchField mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.cancelBtn.width);
    }];
   
    
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self.searchField layoutIfNeeded];
        
//        self.searchField.width = self.width;
    }];
}


#pragma mark - 布局子控件
- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 布局searchField
    [self.searchField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.top.right.bottom.offset(0);
    }];
    
    // 布局cancelBtn
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.top.bottom.offset(0);
        
        make.width.mas_equalTo(50);
    }];
    
    //布局leftView
    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(35);
        
        make.width.mas_equalTo(35);
    }];
}

// 懒加载
-(UITextField *)searchField {
    
    if (nil==_searchField) {
        
        _searchField = [[UITextField alloc] init];
        
//        _searchField.borderStyle = UITextBorderStyleRoundedRect;
        
        _searchField.leftViewMode = UITextFieldViewModeAlways;
        
        _searchField.placeholder = @"听说下雨天和辣条更配哟";
        
        _searchField.borderColor = [UIColor redColor];
        
        _searchField.cornerRadius = 5;
        
        _searchField.borderWidth = 2;
        
        [self addSubview:_searchField];
        
    }
    
    return _searchField;
}


- (UIButton *)cancelBtn {
    
    if (nil==_cancelBtn) {
        
        _cancelBtn = [[UIButton alloc] init];
        
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        
        [_cancelBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
        _cancelBtn.hidden = YES;
        
        [self addSubview:_cancelBtn];
    }
    
    return _cancelBtn;
}


- (UIImageView *)leftView {
    
    if (nil==_leftView) {
        
        // 设置searchField的leftView
        _leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"]];
        
        _leftView.contentMode = UIViewContentModeCenter;
    }
    
    return _leftView;
}

@end
